import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';

export default {
    namespace: 'worker',
    state: {
        worker: {},
        workers: { totalCount: 0, items: [] },
        workerSearchs: { pageIndex: 1, pageSize, keywords: "" }
    },
    effects: {
        /**
         * 获取安装师傅列表
         * @param param0 
         * @param param1 
         */
        *getWorkers({ payload }, { call, put }) {
            const { keywords, pageIndex, pageSize } = payload;
            const formKeywords = keywords, formSkipCount = (pageIndex - 1) * pageSize, formMaxResultCount = pageSize;
            const workers: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallWorkersVOWorkerViewVO =
                yield request(api.ManageworkerManageApiFetchParamCreactor.manageworkerManageGetWorkersBySearchAsync
                    ({
                        formKeywords, formSkipCount, formMaxResultCount
                    }))
            yield put({ type: 'save', payload: { workers, workerSearchs: { keywords, pageIndex, pageSize } } });
        },
        /**
         * 获取安装师傅详情
         * @param param0 
         * @param param1 
         */
        *getWorker({ payload }, { call, put }) {
            const formId = payload.id;
            const worker: api.SingleUserMallWorkersVOWorkerViewVO =
                yield request(api.ManageworkerManageApiFetchParamCreactor.manageworkerManageGetWorkerAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { worker } });
        },
        /**
         * 保存安装师傅
         * @param param0 
         * @param param1 
         */
        *saveWorker({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { workerSearchs } = yield select(({ worker }) => worker);
            yield request(api.ManageworkerManageApiFetchParamCreactor.manageworkerManageSaveWorkerAsync(
                { form }
            ));
            yield put({ type: 'getWorkers', payload: { ...workerSearchs } });
            callback && callback(true);
        },
         /**
         * 删除安装师傅
         * @param param0 
         * @param param1 
         */
        *remove({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { workerSearchs } = yield select(({ worker }) => worker);
            yield request(api.ManageworkerManageApiFetchParamCreactor.manageworkerManageRemoveAsync(
                { form }
            ));
            yield put({ type: 'getWorkers', payload: { ...workerSearchs } });
            callback && callback(true);
        }
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
                switch (pathname) {
                    case '/worker/workers':
                        dispatch({ type: 'getWorkers', payload: { pageIndex: 1, pageSize } });
                        break;
                }
            });
        },
    },
};